<?php
/**
 * @category   Application_Extensions
 * @package    Talktangle Plugin 
 * @copyright  Idragon Co., Ltd.
 * @license
 */
class Talktangle_Widget_CreatecrosstangleController extends Engine_Content_Widget_Abstract {
		
	public function indexAction() {
	   
	    $request = Zend_Controller_Front::getInstance()->getRequest();
        
        $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
        
        $core = Engine_Api::_()->talktangle();
        $userType = $core->getUserType($viewer);
        $this->view->type = $userType;
        $form = new Talktangle_Form_Crosstangle_Create(array('type'=>$userType));
        $this->view->form = $form;
        // Check post
        if(!$request->isPost()){
            return;
        }
     
        if(!$form->isValid($request->getPost())){
            return;
        }
        $values = $_POST;
        $table = Engine_Api::_()->getDbTable('crosstangles','talktangle');
        $adsT = Engine_Api::_()->getDbTable('ads','talktangle');
    
        //save form values
        $db = $table->getAdapter();
        $db->beginTransaction();
        try
        {   
          
            $ads = null;
            if (isset($values['imageId']) && !empty($values['imageId'])) {
                $ads = $adsT->createRow();
                $ads->description = $values['ads_description'];
                $ads->owner_id =$viewer->getIdentity();
                $ads->photo_id = $values['imageId'];
                $ads->save();
            }
            
            
            
            $cross = $table->createRow();
            $cross->title = $values['title'];
            $cross->content = json_encode($values['content']);
            $cross->description = json_encode($values['description']);
            $cross->type = $userType;
            $cross->owner_id = $viewer->getIdentity();
            
            if (isset($values['post_facebook'])) {
                $cross->facebook = $values['post_facebook'];
            }
            if (isset($values['post_tiwtter'])) {
                $cross->tiwtter = $values['post_tiwtter'];
            }
            
            if($userType == 'company'){
                $cross->duration = time()+$values['duration'];
                if($ads){
                    $cross->ad_id = $ads->getIdentity();
                }
            }
            else {
                $durationUser = Engine_Api::_()->getApi('settings','core')->getSetting('talktangle.user.duration',60*10);
                $cross->duration = time()+$durationUser;
                if($ads){
                    $cross->ad_id = $ads->getIdentity();
                }
            }
            $cross->save();
            
            if (!empty($values['promotion_title']))  {
                $promotionTable = Engine_Api::_()->getDbtable('promotions','talktangle');
                $promotion = $promotionTable->createRow();
                $promotion->title = $values['promotion_title'];
                $promotion->description = $values['promotion_description'];
                $promotion->tangle_id = $cross->getIdentity();
                $promotion->photo_id  = $values['promotion_id'];
                $promotion->owner_id = $viewer->user_id;
        
                $promotion->tangle_type = $cross->getType();
                $promotion->save();
                
                $cross->promotion_id = $promotion->getIdentity();
                $cross->save();
                
            } 
            
                
            
            if ( $ads){
               // go to paypal 
                $ads->source_type = $cross->getType();
                $ads->source_id = $cross->getIdentity();
                $ads->save();
                $db->commit();   
                
                
                $paymentApi = Engine_Api::_()->getApi('payment','talktangle');
                try{
                    $a = $paymentApi->paymentPayPal($ads,'pay ads');
                } catch(Exception $e){
                    throw $e;
                }
                
                
            }
            else{
                
                $cross->payment_status = 'complete';
                $cross->save();
                
                $point =  Engine_Api::_()->getApi('settings', 'core')->getSetting('talktangle.point', 2);
                $viewer->points = $viewer->points + $point;
                $viewer->tangle = $viewer->tangle + 1;
                $viewer->save();
                
                $actionT = Engine_Api::_()->getDbtable('actions','activity');
                $action = $actionT->addActivity($viewer,$cross,$cross->getActivityType());
                Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $cross);
                $this->view->url = $url =  Zend_Controller_Front::getInstance()->getRouter()
                        ->assemble(array('controller'=>'index','action'=>'index'), 'talktangle_extended', true);
                
                $db->commit();   
                
                $core = Engine_Api::_()->talktangle();
                if ( isset($_POST['post_facebook']) &&  !empty($_POST['post_facebook']) && $core->enableFacebook() ) {
                    $params = array(
                        'body' => 'had post new crosstangle '.$cross->title,
                        'link' => 'http://' . $_SERVER['HTTP_HOST'].$cross->getHref()
                         
                    );
                   
                    $core->postFacebook($params);
                }
                if ( isset($_POST['post_tiwtter']) &&  !empty($_POST['post_tiwtter'])  && $core->enableTiwtter()) {
                    $params = array(
                        'body' => 'had post new crosstangle '.$cross->title,
                        'link' => 'http://' . $_SERVER['HTTP_HOST'].$cross->getHref()
                         
                    );
                    $core->postTwitter($params);
                }
                
                header("Location: $url"); 
                die;       
                $this->view->success = true;

            }
                              
            
        }
        catch(Exception $e)
        {
            $db->rollBack();
            $this->view->error = $e->getMessage();
            throw $e;
        } 
     
       
	}
}